//
// Created by guo on 24-4-25.
//

#include <iostream>
#include <boost/asio.hpp>
#include <opencv4/opencv2/opencv.hpp>
#include "loader.h"
#include "struct.h"

int main(){
    boost::asio::io_service io_service;
    boost::asio::ip::udp::endpoint local_endpoint(boost::asio::ip::address::from_string("127.0.0.1"),9999);
    boost::asio::ip::udp::endpoint remote_endpoint(boost::asio::ip::address::from_string("127.0.0.1"),8888);
    boost::asio::ip::udp::socket socket(io_service,local_endpoint);

    char buff[512];

    std::vector<std::string> files;
    search_dir("/home/guo/work/cpp/SynergyDemo/data/",files);
    uint32_t i = 0;

    while(1){
        std::cout << "try to recv" << std::endl;
        size_t recv_bytes = socket.receive(boost::asio::buffer(buff,512));
        if(recv_bytes < sizeof(Box)){
            std::cout << "recv size error: " << recv_bytes << std::endl;
            continue;
        }

        Box* box = (Box*)buff;
        std::cout
        << " x: " << box->x
        << " y: " << box->y
        << " w: " << box->w
        << " h: " << box->h
        << " id: " << box->id << std::endl;

        cv::Mat img = cv::imread(files[box->id]);
        cv::Rect rect(box->x,box->y,box->w,box->h);
        cv::Mat cut = img(rect);

        cv::imshow(files[box->id],cut);
        cv::waitKey(500);
    }


    return EXIT_SUCCESS;
}